﻿/********************************************************************************
    Copyright (C) Binod Nepal, Planet Earth Solutions Pvt. Ltd., Kathmandu.
	Released under the terms of the GNU General Public License, GPL, 
	as published by the Free Software Foundation, either version 3 
	of the License, or (at your option) any later version.
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
    See the License here <http://www.gnu.org/licenses/gpl-3.0.html>.
***********************************************************************************/

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace MixNP.Web.Classifieds
{
    public partial class Index : MixNP.BusinessLayer.BaseClass
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            FillCategories();
            ShowCarousel();
        }

        protected void ShowMeButton_Click(object sender, EventArgs e)
        {
            string q = SearchTextBox.Text;
            Response.Redirect(ResolveUrl("~/search?q=" + HttpUtility.UrlEncode(q)));
        }

        private void ShowCarousel()
        {
            this.BuildCarousel("mycarousel", MixNP.BusinessLayer.Items.GetLatestItems(), CaraoselLiteral);
            this.BuildCarousel("mycarousel2", MixNP.BusinessLayer.Items.GetPopularItems(), MostPopularLiteral);
        }

        private void BuildCarousel(string carouselId, System.Data.DataTable table, Literal l)
        {
            StringBuilder html = new StringBuilder();

            if (table.Rows.Count > 0)
            {
                html.Append("<ul id=\"" + carouselId + "\" class=\"jcarousel-skin-tango\">");

                double itemId = double.MinValue;
                string itemUrl = string.Empty;
                string title = string.Empty;
                decimal price = decimal.MinValue;
                string image = string.Empty;

                foreach (DataRow row in table.Rows)
                {
                    title = row["Title"].ToString();
                    price = Pes.Utility.Conversion.TryCastDecimal(row["Price"]);
                    image = row["FilePath"].ToString();
                    itemId = Pes.Utility.Conversion.TryCastDouble(row["ItemId"]);
                    itemUrl = MixNP.BusinessLayer.Items.GetItemUrl(itemId, this.Page);


                    html.Append("<li>");
                    html.Append("<div onclick=\"window.location='" + itemUrl + "'\">");

                    html.Append("<div class=\"fly-header\">");
                    html.Append(title);
                    html.Append("</div>");
                    html.Append("<img alt=\"" + title + "\" src=\"/Handlers/GetItemImage.ashx?Path=" + image + "&W=160&H=170\" style=\"padding: 4px; margin: 4px; border: 1px solid #DEDEDE;\" />");
                    html.Append("<br />");

                    html.Append("<input type=\"button\" value=\"For Rs. " + price.ToString("G29") + "\" />");

                    html.Append("</div>");
                    html.Append("</li>");

                }

                html.Append("</ul>");
                l.Text = html.ToString();

            }
        }

        protected void StartButton_Click(object sender, EventArgs e)
        {
            int categoryId = Pes.Utility.Conversion.TryCastInteger(CategoryDropDownList.SelectedValue);
            string alias = MixNP.BusinessLayer.Categories.GetCategoryAlias(categoryId);
            Response.Redirect("~/sell/" + "/" + alias + ".mix", true);
        }



        private void FillCategories()
        {
            string left = string.Empty, middle = string.Empty, right = string.Empty;
            string html = string.Empty;

            int counter = 0;
            using (DataTable parentCategories = MixNP.BusinessLayer.Categories.GetRootCategories())
            {
                foreach (DataRow row in parentCategories.Rows)
                {
                    #region "Counter"
                    counter++;
                    if (counter == 4)
                    {
                        counter = 1;
                    }
                    #endregion

                    int categoryId = Pes.Utility.Conversion.TryCastInteger(row["CategoryId"]);
                    string url = ResolveUrl("~/view/" + row["alias"].ToString() + ".mix");


                    html = "<div class=\"category\">";
                    html += "<div class=\"category-header\">";
                    html += "<a href=\"{0}\" title=\"{1}\">{1}</a>";
                    html += "</div>";

                    html = string.Format(html, url, row["CategoryName"].ToString());
                    html += "<ul>";

                    using (DataTable categories = MixNP.BusinessLayer.Categories.GetCategories(categoryId))
                    {
                        foreach (DataRow childRow in categories.Rows)
                        {
                            categoryId = Pes.Utility.Conversion.TryCastInteger(childRow["CategoryId"]);
                            url = ResolveUrl("~/view/" + row["alias"].ToString() + "/" + childRow["alias"].ToString() + ".mix");

                            html += "<li>";
                            html += "<a href=\"{0}\" title=\"{1}\">{1} ({2})</a>";
                            html += "</li>";
                            html = string.Format(html, url, childRow["CategoryName"].ToString(), childRow["TotalItems"].ToString());
                        }
                    }

                    html += "</ul></div>";

                    if (counter.Equals(1))
                    {
                        left += html;
                    }
                    else if (counter.Equals(2))
                    {
                        middle += html;
                    }
                    else
                    {
                        right += html;
                    }
                }
            }

            LeftLiteral.Text = left;
            MiddleLiteral.Text = middle;
            RightLiteral.Text = right;

        }
    }
}